package com.boomsecret.demo;

import com.boomsecret.domain.InfoDomain;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Created by wangjinliang on 2018/1/15.
 */
public class JDBCDemo {

    private static List<InfoDomain> domains = null;

    public static void main(String[] args) {
        ResourceBundle rb = ResourceBundle.getBundle("db");
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String username = rb.getString("jdbc.username");
        String password = rb.getString("jdbc.password");
        try{
            Class.forName(driver);
            Connection connect = DriverManager.getConnection(url, username, password);
            Statement statement = connect.createStatement();
            String sql = "select id, link from info";
            ResultSet rs = statement.executeQuery(sql);
            domains = new ArrayList<>();
            while(rs.next()){
                InfoDomain domain = new InfoDomain();
                domain.setId(rs.getInt("id"));
                domain.setLink(rs.getString("link"));
                domains.add(domain);
            }
            rs.close();
            connect.close();

            // 解析
            domains.forEach(domain -> {
                try {
                    Document doc = Jsoup.connect(domain.getLink()).get();
                    Element link = doc.select(".video_iframe").first();
                    if (link != null) {
                        System.out.println("update info set video_status = 1 where id = " + domain.getId() + ";");
                    } else {
                        System.out.println("id:" + domain.getId() + "  link:" + domain.getLink());
                    }
                } catch (Exception e) {
                    System.out.println("Jsoup Exception:" + e.getMessage());
                    System.out.println("id:" + domain.getId() + "  link:" + domain.getLink());
                }
            });
        }catch(Exception e){
            System.out.println(e);
        }
    }
}
